package com.scud.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.scud.common.error.ErrorConstants;
import com.scud.common.exception.ScudException;
import com.scud.model.system.dto.PermissionDto;
import com.scud.model.system.pojo.Permission;
import com.scud.system.mapper.PermissionMapper;
import com.scud.system.mapper.RolePermissionMapper;
import com.scud.system.service.PermissionService;
import com.scud.system.utils.TreeUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class PermissionServiceImpl implements PermissionService {
    private final PermissionMapper permissionMapper;
    private final RolePermissionMapper rolePermissionMapper;

    /**
     * 获取所有权限的树形集合
     * @return
     */
    @Override
    public List<Permission> getPermissions() {
        List<Permission> list = permissionMapper.getAll();
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return TreeUtil.buildTree(list);
    }

    /**
     * 获取 所有权限的树形集合 和 roleId对应的权限id集合
     * allMenus          roleMenuIds
     *
     * @param roleId
     * @return
     */
    @Override
    public Map<String, Object> getAllPermissions(Long roleId) {
        List<Permission> allMenus = getPermissions();
        List<Long> roleMenuIds = rolePermissionMapper.getPermissionIdsByRoleId(roleId);
        Map<String, Object> map = new HashMap<>();
        map.put("allMenus", allMenus);
        map.put("roleMenuIds", roleMenuIds);
        return map;
    }

    /**
     * 新增权限
     * @param permissionDto
     */
    @Override
    @Transactional
    public void save(PermissionDto permissionDto) {
        PermissionDto m = permissionMapper.getByComponent(permissionDto.getComponent());
        if (ObjectUtil.isNull(m)) {
            if (permissionDto.getParentId() == null) {
                permissionDto.setParentId(0);
            }
            permissionMapper.add(permissionDto);
            updateRoleMenuIsHalf(permissionDto);
        } else {
            throw new ScudException(ErrorConstants.COMPONENT_EXIST);
        }
    }

    /**
     * 将其父权限整体改为半开
     * @param permissionDto
     */
    private void updateRoleMenuIsHalf(PermissionDto permissionDto) {
        PermissionDto permission = permissionMapper.getByParentId(permissionDto.getParentId());
        if (ObjectUtil.isNotNull(permission)) {
            rolePermissionMapper.updateRolePermissionIsHalf(permission.getId());
            updateRoleMenuIsHalf(permission);
        }
    }

    /**
     * 修改权限
     * @param permissionDto
     */
    @Override
    public void update(PermissionDto permissionDto) {
        permissionMapper.update(permissionDto);
    }

    /**
     * 删除权限
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        rolePermissionMapper.deleteByPermissionId(id);
        permissionMapper.delete(id);
    }
}
